package flib;

import java.util.Collections;
import java.util.Vector;

import simpleGeom.HENode;
import simpleGeom.HasPos2D;
import simpleGeom.Polygon2D;
import simpleGeom.Vec2D;


public class SplitArea extends Polygon2D {
Vector<SplitArea>children=new Vector<SplitArea>();
Split split;
boolean checked;
Vector<Pair>borders;
public void collectAllLeaves(Vector<SplitArea>leaves){
	if (children.size()==0){
		leaves.add(this);
		return;
	}
	for (int i=0;i<children.size();i++){
		SplitArea sA=children.get(i);
		sA.collectAllLeaves(leaves);
	}
}
public Vector<SplitArea> split(Split split){
	Vector<SplitArea>newAreas=new Vector<SplitArea>();
	int id=0;
	Vector<HasPos2D>areaPts=this.getPts();
	Vector<HasPos2D>splitpts=split.getPts();
	int s1=-1;
	int s2=-1;
	int i1=-1;
	int i2=-1;
	for (int i=0;i<splitpts.size();i++){
		HENode p=(HENode) splitpts.get(i);
		int cI=areaPts.indexOf(p);
		if (cI>=0){
			if (s1<0){
				i1=i;
				s1=cI;
			}
			else{
				i2=i;
				s2=cI;
				break;
			}
		}
	}
	System.out.println("i1: "+i1);
	System.out.println("i2: "+i2);
	System.out.println("s1: "+s1);
	System.out.println("s2: "+s2);
	System.out.println("areaPts: "+areaPts.size());
	SplitArea split1=new SplitArea();
	for (int i=i1+1;i<i2;i++){
		split1.addPt(splitpts.get(i));
	}
	System.out.println("pts ");
	int sI=s2;
	while(sI!=s1){
		split1.addPt(areaPts.get(sI));
		sI++;
		sI=sI%this.getNPts();
	}
	split1.addPt(areaPts.get(s1));
	System.out.println("face1 ");
	SplitArea split2=new SplitArea();
	for (int i=i2-1;i>i1;i--){
		split2.addPt(splitpts.get(i));
	}
	sI=s1;
	while(sI!=s2){
		split2.addPt(areaPts.get(sI));
		sI++;
		sI=sI%this.getNPts();
	}
	split2.addPt(areaPts.get(s2));
	System.out.println("face2 ");
	children.add(split1);
	children.add(split2);
	newAreas.add(split1);
	newAreas.add(split2);
	this.split=split;

	return newAreas;
}
Pair startP;
public void setCornerPts(){
	Vector<Pair>pairs=new Vector<Pair>();
	borders=new Vector<Pair>();
	for (int i=0;i<this.getNPts();i++){
		int i1=i;
		int i2=(i+1)%getNPts();
		int i3=(i+2)%getNPts();
		HasPos2D p1=this.get(i1);
		HasPos2D p2=this.get(i2);
		HasPos2D p3=this.get(i3);
		float angle=Vec2D.getAngleBetween(p1, p2, p3);
		Pair pair=new Pair();
		pair.p=p2;
		pair.id=i;
		pair.angle=angle;
		pairs.add(pair);
	}
	Collections.sort(pairs);
	Pair maxP1=null;
	Pair maxP2=null;
	for (int i=0;i<4;i++){
		Pair cP=pairs.get(i);
		if (maxP1==null||maxP1.p.getY()<cP.p.getY()){
			maxP1=cP;
		}
		else if (maxP2==null||maxP2.p.getY()<cP.p.getY()){
			maxP2=cP;
		}
		borders.add(cP);
	}
	startP=maxP1;
	//Vector<HasPos2D>
	
	if (startP.p.getX()<maxP2.p.getX()){
		startP=maxP2;
	}
}


public boolean contains(Split split){
	int id=0;
	// how to detect a real split...
	Vector<HasPos2D>splitPts=split.getPts();
	int i1=-1;
	int i2=-1;
	
	// contains...
	// genau zwei punkte im ring. 
	// beide punkte mit abstand im Ring
	
	
	for (int i=0;i<splitPts.size();i++){
		HENode p=(HENode) splitPts.get(i);
		int cI=this.getPts().indexOf(p);
		if (cI>=0){
			id++;
			if (i1<0){
				i1=cI;
			}
			else if (i2<0){
				i2=cI;
			}
			else return false;//mehr als 2 punkte
		}
		
	}
	if (id!=2)return false;
	if (((i1+1)%this.getPts().size())==i2)return false;
	if (((i2+1)%this.getPts().size())==i1)return false;
	return true;
}

public class Pair implements Comparable{
	int id;
	HasPos2D p;
	float angle;
	public int compareTo(Object arg0) {
		// TODO Auto-generated method stub
		Pair pair=(Pair)arg0;
		return Float.compare(angle, pair.angle);
	}
}
}
